package com.keller.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keller.guli.service.edu.entity.Chapter;
import com.keller.guli.service.edu.entity.Video;
import com.keller.guli.service.edu.entity.vo.ChapterVo;
import com.keller.guli.service.edu.entity.vo.VideoVo;
import com.keller.guli.service.edu.mapper.ChapterMapper;
import com.keller.guli.service.edu.mapper.VideoMapper;
import com.keller.guli.service.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author keller
 * @since 2020-07-21
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public boolean removeChapterById(String id) {
        //课时信息：video，controller里是把视频删了数据库里的数据没删，这里删除数据库里的数据
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id", id);
        videoMapper.delete(videoQueryWrapper);
        //章节信息：chapter
        return this.removeById(id);
    }

    @Override
    public List<ChapterVo> nestedList(String courseId) {
        //根据courseId查询所有的章节
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        chapterQueryWrapper.orderByAsc("sort", "id");
        List<Chapter> chapterList = baseMapper.selectList(chapterQueryWrapper);

        //根据章节查询所有的videovo(课时)
        QueryWrapper<Video> queryWrapperVideo = new QueryWrapper<>();
        queryWrapperVideo.eq("course_id", courseId);
        queryWrapperVideo.orderByAsc("sort", "id");
        List<Video> videoList = videoMapper.selectList(queryWrapperVideo);

        //填充列表数据：Chapter列表-->填充videovo数据
        List<ChapterVo> chapterVoList = new ArrayList<>();
        for(int i=0; i<chapterList.size(); i++){
            ChapterVo chapterVo = new ChapterVo();
            Chapter chapter = chapterList.get(i);
            BeanUtils.copyProperties(chapter, chapterVo);

            //填充课时数据
            List<VideoVo> videoVoList = new ArrayList<>();
            for(int j=0; j<videoList.size(); j++){
                Video video = videoList.get(j);
                if(video.getChapterId().equals(chapter.getId())){
                    System.out.println("video = " + video);
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoList.add(videoVo);
                    System.out.println("videovo = " + videoVo);
                }
            }

            chapterVo.setChildren(videoVoList);

            chapterVoList.add(chapterVo);
        }

        return chapterVoList;
    }
}
